package com.wwwarehouse.usercenter.fragment.chain_triangles;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.share.ShareContent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.response.InviteInfoResponseBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;
import com.wwwarehouse.usercenter.fragment.invite.InviteDetailsFragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.content.ContentValues.TAG;

public class TCInviteDetailsFragment extends InviteDetailsFragment {

    private Dialog mDialog;
    private BaseApplication application;
    private ShareContent content;
    private int currentSendType;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        application = BaseApplication.getApplicationInstance();

        Bundle bundle = getArguments();
        if(bundle.getParcelable("content") != null ){
            content = bundle.getParcelable("content");
        }

    }

    @Override
    public void adapterListener(final List<InviteInfoResponseBean.InviteDetailsBean> inviteDetailList) {
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mInviteDetailsBean = inviteDetailList.get(position);
                String  manageType = getArguments().getString("manageType").equals("regulator")?getResources().getString(R.string.level_managers):getResources().getString(R.string.level_monitoring);
                DialogTools.getInstance().showTCDialog(getActivity(), "",
                        getResources().getString(R.string.confirm_specified) + mInviteDetailsBean.getInviteeName() + getResources().getString(R.string.for_) + manageType, getResources().getString(R.string.confirm_specified), getResources().getString(R.string.consider),
                        new DialogTools.ConfirmListener() {
                            @Override
                            public void setConfirmListener(Dialog dialog, View view, String s) {
                                assignManager(mInviteDetailsBean.getInviteeId());
                                dialog.dismiss();
                            }
                        }, new DialogTools.DismissListener() {
                            @Override
                            public void setDismissListener(Dialog dialog, View view) {
                                dialog.dismiss();
                            }
                        });

            }
        });
    }

    /**
     * 三角链-指定一级管理者/监控者
     */
    private void assignManager(String inviteeId) {
        Map<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("assignType", "mobile");//APP 网仓APP扫一扫 mobile 站内通讯录指定 oneself 指定给自己
        hashMap.put("buId", getArguments().getString("buId"));//assignType=mobile,oneself必填
        hashMap.put("inviteeId", inviteeId);//被邀请人ID assignType=mobile必填
        hashMap.put("manageType", getArguments().getString("manageType"));//管理类型 assignType=mobile,oneself必填 （supervisor一级监控者 regulator一级管理者）
        NoHttpUtils.httpPost(UserCenterConstant.ASSIGN_MANAGER, hashMap, onResponseListener, 0);
        showProgressDialog();
    }

    NoHttpUtils.OnResponseListener onResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case 0:
                    if (TextUtils.equals("0", commonClass.getCode())) {
                        if (getArguments().getString("manageType").equals("regulator")) {
                            TriangulationChainFragment.hasAT = true;
                        } else {
                            TriangulationChainFragment.hasSV = true;
                        }
                        TCSuccessFragment tcSuccessFragment = new TCSuccessFragment();
                        Bundle bundle = new Bundle();
                        bundle.putInt("type", Constant.TYPE_ADDRESS);
                        bundle.putSerializable("bean", mInviteDetailsBean);
                        bundle.putSerializable("from", getArguments().getInt("from",0));

                        tcSuccessFragment.setArguments(bundle);
                        popFragment();
                        ((BaseCardDeskActivity) getActivity()).pushFragment(tcSuccessFragment);
                    } else {

                    }
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            Log.d(TAG, result);
        }

        @Override
        public void onFinish(int what) {
            dismissProgressDialog();
        }
    };

    @Override
    public void setTitle() {
        if (peekFragment() instanceof TCInviteDetailsFragment) {
            mActivity.setTitle(getResources().getString(R.string.set_triangular_management));
        }
    }

}
